﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Confucius.utils;
using Confucius.entity;
using Confucius.daoImpl;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;

namespace Confucius.pages
{
    public partial class ObjectDetailPage : PhoneApplicationPage
    {
        string currentGameID;
        string objectID;

        public ObjectDetailPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ObjectDetailPage_Loaded);
        }

        private void ObjectDetailPage_Loaded(object sender, RoutedEventArgs e)
        {
            objectID = "";
            currentGameID = "";

            if (IsolatedStorageSettings.ApplicationSettings.Contains(Constants.CURRENT_GAMEID))
            {
                currentGameID = (string)IsolatedStorageSettings.ApplicationSettings[Constants.CURRENT_GAMEID];
            }
            if (currentGameID == "")
            {
                MessageBox.Show("Cannot find matched game, please contact team Confucius.");
                NavigationService.GoBack();
            }

            if (NavigationContext.QueryString.ContainsKey("questID"))
            {
                objectID = NavigationContext.QueryString["questID"];
                if (objectID != null)
                {
                    Quest quest = new QuestDAOImpl().readQuest(objectID, currentGameID);
                    ObjectName.Text = quest.Title;
                    //Uri uri = ConfuciusUtil.buildImageUri(currentGameID, quest.IconID);
                    //ObjectImage.Source = new BitmapImage(uri);
                    ObjectImage.Visibility = Visibility.Collapsed;
                    ObjectDesc.Text = quest.CompleteText;
                    ObjectExtra1Title.Text = Constants.OBJECT_QUEST_ORDER;
                    ObjectExtra1Title.Visibility = Visibility.Visible;
                    ObjectExtra1Content.Text = Convert.ToString(quest.QuestOrder);
                    ObjectExtra1Content.Visibility = Visibility.Visible;
                }
            }
            else if (NavigationContext.QueryString.ContainsKey("npcID"))
            {
                objectID = NavigationContext.QueryString["npcID"];
                if (objectID != null)
                {
                    NPC npc = new NPCDAOImpl().readNPC(objectID, currentGameID);
                    ObjectName.Text = npc.Name;

                    string[] iconString = npc.FigureID.Split('.');
                    if (iconString.Length < 2)
                    {
                        Uri uri = ConfuciusUtil.buildImageUri(currentGameID, npc.FigureID);
                        ObjectImage.Source = new BitmapImage(uri);
                        ObjectImage.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        if (iconString[1].Equals("png"))
                        {
                            Uri uri = ConfuciusUtil.buildImageUriWithExtension(currentGameID, npc.FigureID);
                            ObjectImage.Source = new BitmapImage(uri);
                            ObjectImage.Visibility = Visibility.Visible;
                        }
                    }

                    ObjectDesc.Text = npc.Greeting;
                    ObjectExtra1Title.Visibility = Visibility.Collapsed;
                    ObjectExtra1Content.Visibility = Visibility.Collapsed;
                }
            }
            else if (NavigationContext.QueryString.ContainsKey("itemID"))
            {
                objectID = NavigationContext.QueryString["itemID"];
                if (objectID != null)
                {
                    Item item = new ItemDAOImpl().readItem(objectID, currentGameID);
                    ObjectName.Text = item.Title;

                    string[] mediaString = item.ContainedMediaID.Split('.');
                    if (mediaString.Length > 1)
                    {
                        if (mediaString[1].Equals("png"))
                        {
                            Uri uri = ConfuciusUtil.buildImageUriWithExtension(currentGameID, item.ContainedMediaID);
                            ObjectImage.Source = new BitmapImage(uri);
                            ObjectImage.Visibility = Visibility.Visible;
                        }
                        else if (mediaString[1].Equals("mp4") || mediaString[1].Equals("wmv") ||
                            mediaString[1].Equals("mp3"))
                        {
                            Uri imageSource = new Uri("/Confucius;component/assets/ui/PlayNormal.png", UriKind.Relative);
                            PlayButton.Source = new BitmapImage(imageSource);
                            PlayButton.Visibility = Visibility.Visible;
                            Uri uri = ConfuciusUtil.buildVideoBackgroundUri(Constants.ITEM_MEDIA_BACKGROUND);
                            ObjectImage.Source = new BitmapImage(uri);
                            ObjectImage.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            ObjectImage.Visibility = Visibility.Collapsed;
                            PlayButton.Visibility = Visibility.Collapsed;
                        }
                    }
                    else
                    {
                        ObjectImage.Visibility = Visibility.Collapsed;
                        PlayButton.Visibility = Visibility.Collapsed;
                    }
                    ObjectDesc.Text = item.DisplayText;
                    ObjectExtra1Title.Visibility = Visibility.Collapsed;
                    ObjectExtra1Content.Visibility = Visibility.Collapsed;
                }
            }

            if (objectID == "")
            {
                ObjectName.Text = Constants.OBJECT_NO_ID_TITLE;
                ObjectImage.Visibility = Visibility.Collapsed;
                ObjectDesc.Text = Constants.OBJECT_NO_ID_DESCRIPTION;
                ObjectExtra1Title.Visibility = Visibility.Collapsed;
                ObjectExtra1Content.Visibility = Visibility.Collapsed;
            }
        }
        private void ImageOnclick(object sender, RoutedEventArgs e)
        {
            //task.ShowLocation();
        }

        private void Back_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void MediaClickDown(object sender, RoutedEventArgs e)
        {

            if (NavigationContext.QueryString.ContainsKey("itemID"))
            {
                Item item = new ItemDAOImpl().readItem(objectID, currentGameID);
                string itemMedia = item.ContainedMediaID;
                string[] mediaString = itemMedia.Split('.');
                if (mediaString.Length > 1)
                {
                    if (mediaString[1].Equals("wmv") || mediaString[1].Equals("mp4") ||
                        mediaString[1].Equals("mp3"))
                    {
                        Uri imageSource = new Uri("/Confucius;component/assets/ui/PlayPressed.png", UriKind.Relative);
                        PlayButton.Source = new BitmapImage(imageSource);
                        PlayButton.Visibility = Visibility.Visible;
                        Uri uri = ConfuciusUtil.buildMediaUri(currentGameID, item.ContainedMediaID);
                        MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
                        mediaPlayerLauncher.Location = MediaLocationType.Install;
                        mediaPlayerLauncher.Media = uri;

                        mediaPlayerLauncher.Show();

                    }

                }
            }
        }

        private void MediaClickUp(object sender, RoutedEventArgs e)
        {

            if (NavigationContext.QueryString.ContainsKey("itemID"))
            {
                Item item = new ItemDAOImpl().readItem(objectID, currentGameID);
                string itemMedia = item.ContainedMediaID;
                string[] mediaString = itemMedia.Split('.');
                if (mediaString.Length > 1)
                {
                    if (mediaString[1].Equals("wmv") || mediaString[1].Equals("mp4") ||
                        mediaString[1].Equals("mp3"))
                    {
                        Uri imageSource = new Uri("/Confucius;component/assets/ui/PlayNormal.png", UriKind.Relative);
                        PlayButton.Source = new BitmapImage(imageSource);
                        PlayButton.Visibility = Visibility.Visible;

                    }

                }
            }
        }

    }
}